package com.xilai.cuteBoy.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xilai.cuteBoy.service.UserServiceInter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@Controller
@RequestMapping(value = "/user")
public class UserController {

    private static final Logger _log = LoggerFactory.getLogger(UserController.class);

    @Resource
    private UserServiceInter userServiceImpl;

    @RequestMapping(value = "addUser",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject addUser(@RequestBody JSONObject json){
        _log.info("添加新用户传入参数是："+json.toString());
        JSONObject jsonObject = new JSONObject();
        //参数解析
        String username = json.getString("username");
        String password = json.getString("password");
        int row = userServiceImpl.addUser(username,password);
        if (1 == row){
            jsonObject.put("msg","success");
            return jsonObject;
        }
            jsonObject.put("msg","failure");
            return jsonObject;
    }

    @RequestMapping(value = "getAllUser",method = RequestMethod.POST)
    @ResponseBody
    public JSONArray getAllUser(@RequestBody JSONObject json){
        int page = json.getInteger("page");
        int pageSize = json.getInteger("pageSize");
        Date date1 = new Date();
        JSONArray jsonArray = userServiceImpl.getAllUser(page,pageSize);
        Date date2 = new Date();
        if (null == jsonArray || jsonArray.size() == 0){
            return null;
        }
        return  jsonArray;
    }

    @RequestMapping(value = "/toTestPage",method = {RequestMethod.GET,RequestMethod.POST})
    public String toTestPage(){
        return "redirect:/user/toTestPage1";
    }

    @RequestMapping(value = "/toTestPage1",method = RequestMethod.GET)
    public String toTestPage1(){
        System.out.println(";lk;ldsfgkld;l");
        return "Test";
    }

}
